//
//  SignatureEditViewController.Action.swift
//  App_Profile
//
//  Created by 蒋晨成 on 2022/2/11.
//

import Center
import Foundation
import UICore
import UIKit

extension SignatureEditViewController {
    func bind() {
        signatueTextView.rx.text.orEmpty.asObservable()
            .subscribe(onNext: { [self] text in
                self.countLabel.text = "\(self.maxSignatureLength - text.count)"
            })
            .disposed(by: disposeBag)
    }

    @objc
    func confirm() {
        guard let signature = signatueTextView.text, !signature.isEmpty else {
            view.makeToast(ProfileModular.localizedString(key: "Profile.Userinfo.Signature.Empty"), position: .center)
            return
        }

        if signature == UserCenter.userInfo?.signature {
            back()
            return
        }
        view.makeToastActivity(.center)
        UserCenter.updateBasicProfile(signature: signature) { [weak self] result in
            self?.view.hideToastActivity()
            switch result {
            case .success:
                self?.back()
            case .failure(let error):
                self?.view.makeToast(error.localizedDescription, position: .center)
            }
        }
    }
}

extension SignatureEditViewController: UITextViewDelegate {}
